﻿package base.webcam {
	import flash.display.Bitmap;
	import flash.media.Video;
	import flash.geom.Matrix;
	import flash.media.Camera;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.events.Event;

	public class BmpWebcamCapture extends Bitmap {
		
		public var video: Video;
		private var mrxMirror: Matrix;
		
		public function BmpWebcamCapture(width: uint, height: uint, fps: uint = 30): void {
			super(new BitmapData(width, height, false, 0), PixelSnapping.AUTO, true);
			var webcam: Camera = Camera.getCamera();
			if (!webcam) throw new Error('No webcam!!!!');
			webcam.setMode(width, height, fps);
			this.video = new Video(width, height);
			this.video.attachCamera(webcam);
			this.video.scaleX = -1;
			this.video.x = width;
			this.mrxMirror = new Matrix();
			this.mrxMirror.scale(-1, 1);
			this.mrxMirror.translate(width, 0);
			this.addEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
		}
		
		protected function onEnterFrameHandler(e: Event):void {
			this.bitmapData.draw(this.video, this.mrxMirror);
		}
		
	}

}